import {
	get,
	add,
	update,
	remove,
	query,
	queryAll
} from "../../services/quotationSupport/chargeTemplate";
import Constants from '../../utils/Constants'
import { message } from "antd";

const R = require('ramda');

export default {
	namespace: 'chargeTemplate',
	state: {
		page: {},
		list: [],
		dataSource: {}

		},
	reducers: {
		refreshState(state, { payload }) {
			return { ...state, ...payload };
		}
	},
	effects: {
		*query({ payload }, { call, put }) {
			const res = yield call(query, { ...R.omit(['onSuccess', 'onFail'])(payload) });
			if (res && res.data.resultCode === "ok") {
				yield put({
					type: 'refreshState',
					payload: {
						list: res.data.content.list,
						page: res.data.content.page
					}
				});
			} else {
				if (payload.onFail) payload.onFail();
			}
		},

        *queryAll({ payload }, { call, put }) {
            const res = yield call(queryAll, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list,
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
		*add({ payload }, { call }) {
            let dataSource = payload.dataSource.charges ? { ...R.omit(['onSuccess', 'onFail'])(payload) } : R.clone(payload.dataSource)
            // let dataSource = R.clone(payload.dataSource);
                let charges = dataSource && dataSource.charges ? dataSource.charges : [];
                charges.map((feeTerm) => feeTerm.id && R.contains(Constants.TEMP_ID_PRE, feeTerm.id) ? feeTerm.id = null : feeTerm);
                const res = yield call(add, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if (res && res.data.resultCode === "ok") {
                    message.info("新增成功！");
                    if (payload.onSuccess) payload.onSuccess();
                } else {
                    if (payload.onFail) payload.onFail();
                }
		},

		*remove({ payload }, { call }) {
			const res = yield call(remove, { ...R.omit(['onSuccess', 'onFail'])(payload) });
			if (res && res.data.resultCode === "ok") {
				message.info("删除成功！");
				if (payload.onSuccess) payload.onSuccess();
			} else {
				if (payload.onFail) payload.onFail();
			}
		},

		*update({ payload }, { call, put }) {
            let charges = payload.dataSource && payload.dataSource.charges ? payload.dataSource.charges : [];
            charges.map((feeTerm) => R.contains(Constants.TEMP_ID_PRE, feeTerm.id) ? feeTerm.id = null : feeTerm);

            const res = yield call(update, { ...R.omit(['onSuccess', 'onFail'])(payload) });
			if (res && res.data.resultCode === "ok") {
				message.info("更新成功！");
				if (payload.onSuccess) payload.onSuccess();
			} else {
				if (payload.onFail) payload.onFail();
			}
		},

		*get({ payload }, { call, put }) {
			if (payload.id) {
				const res = yield call(get, { ...R.omit(['onSuccess', 'onFail'])(payload) });
				if (res && res.data.resultCode === "ok") {
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content,
                        }
                    });
                    if (payload.onSuccess) payload.onSuccess();
                } else {
                    if (payload.onFail) payload.onFail();
				}

        } else {
            yield put({
                type: 'refreshState',
                payload: {
                    dataSource: {}
                }
            });
}
		},
	},
	subscriptions: {}
};
